﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Utilities
{

    public class DateUtils
    {

        public static DateTime? parseROCDate(String s)
        {
            String[] ss=s.Trim().Split('/');
            if (ss.Length != 3)
                return null;
            int y=int.Parse(ss[0])+1911;
            int m = int.Parse(ss[1]);
            int d = int.Parse(ss[2]);
            DateTime date = new DateTime(y, m, d);
            return date;
        }
        public static DateTime getValidTradeDate(DateTime d)
        {
            if (d.DayOfWeek.Equals(DayOfWeek.Saturday))
            {
                return d.AddDays(-1);
            }
            if (d.DayOfWeek.Equals(DayOfWeek.Sunday))
            {
                return d.AddDays(-2);
            }
            if (d.Hour < 14)
            {
                d = d.AddHours(15 - d.Hour);
                return getValidTradeDate(d.AddDays(-1));
            }
            return d;
        }

        public static DateTime getDateOnly(DateTime d)
        {
            return DateTime.Parse(String.Format("{0}/{1}/{2}", d.Year, d.Month, d.Day));
        }
        public static String getYYYYMMDD_NoSlash(DateTime d)
        {
            return d.ToString("yyyyMMdd");
        }
        public static String getYYYYMMPlanString(DateTime d)
        {
            return String.Format("{0}{1:00}", d.Year, d.Month);
        }
        public static String getPlanString(DateTime d)
        {
            return String.Format("{0}{1:00}{2:00}", d.Year, d.Month, d.Day);
        }
        public static String toYYYYMMDD_Slash(DateTime d)
        {
            return d.ToString("yyyy/MM/dd");
        }
        public static int GetWeekNumber(DateTime dtPassed)
        {
            CultureInfo ciCurr = CultureInfo.CurrentCulture;
            int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            return weekNum;
        }
        public static YMD getYMD(DateTime date)
        {
            YMD ymd = new YMD();
            ymd.year = date.Year;
            ymd.month = date.Month;
            ymd.day = date.Day;
            ymd.yyyy = ymd.year.ToString();
            ymd.yy = (ymd.year - 1911).ToString();
            ymd.MM = String.Format("{0:00}", ymd.month);
            ymd.dd = String.Format("{0:00}", ymd.day);
            return ymd;
        }
        public static String getROCString(DateTime d)
        {
            return String.Format("{0}/{1:00}/{2:00}", d.Year - 1911, d.Month, d.Day);
        }

    }
}
